package com.cwm.test;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.*;

public class ObjectOutStream {
    public static void main(String[] args) {
        //如果想要序列化某个内容到某个文件，首先要创建一个文件
        //注意：序列化后保存的内容，不是存文本，而是按照他本来的格式来保存的
        String filePath = "C:\\test.bat";
 
        try {
            //创建序列化对象
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));
            //序列化数据到e:\test.bat
            oos.writeInt(100); //int类型的数字会在传入时进行装箱，转化Integer包装类（Integer实现了Serializable接口）
            oos.writeDouble(1.2);//double类型—>Double包装类（Double包装类实现了Serializable接口）
            oos.writeChar('S');//char -> Character包装类（Character实现了Serializable接口）
            oos.writeBoolean(true); // boolean ->Boolean包装类（Boolean实现了Serializable接口）
            oos.writeUTF("许国辉一定可以！"); //字符串String本身就实现了Serializable接口
 
            //序列化一个对象
            oos.writeObject(new Dog("旺财",10));
 
            //用完这个序列化对象记得关闭，释放资源
            oos.close();
 
            //验证是否序列化成功
            System.out.println("序列化完成~~~~~");
            ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(filePath));
            System.out.println(objectInputStream.readInt());
            System.out.println(objectInputStream.readDouble());
            System.out.println(objectInputStream.readChar());
            System.out.println(objectInputStream.readBoolean());
            System.out.println(objectInputStream.readUTF());
            System.out.println(objectInputStream.readObject());
            HttpServletResponse response= null;
//            response.getOutputStream()
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
        }
 
 
    }
 
    static class Dog implements Serializable {
        private String name;
        private int age;
 
        public Dog(String name, int age) {
            this.name = name;
            this.age = age;
        }
        @Override
        public String toString(){
        return "Dog{" +
                "name= '" +name +"\'" +
                "age= '"+ age +"\'" +
                "}";
        }
    }
}